﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class ReturnResult : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();

                if (userModel != null && userModel.RoleID.Equals("1"))
                {
                    btnDelete.Visible = true;
                }
            }
        }

        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = "SELECT * FROM T_ReturnTemp WHERE 1=1";

            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(txtIMSI.Text.Trim()))
                strWhere += string.Format(" AND IMSI LIKE '%{0}%'", txtIMSI.Text.Trim());
            if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                strWhere += string.Format(" AND CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            strSQL += strWhere;

            strSQL += " ORDER BY CreateTime DESC";

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

        }
        private void GVDataBindAll()
        {
            string strSQL = "SELECT * FROM T_ReturnTemp ORDER BY CreateTime DESC";

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;
        }

        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "请求时间From必须小于等于To！");
                        return;
                    }
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                inputDateFrom.Value = "";
                inputDateTo.Value = "";
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "Delete FROM T_ReturnTemp a Where 1=1";

                if (!string.IsNullOrEmpty(txtIMSI.Text.Trim()))
                    strSQL += string.Format(" AND IMSI LIKE '%{0}%'", txtIMSI.Text.Trim());
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                    strSQL += string.Format(" AND CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

                DbHelperSql.ExecuteSql(strSQL);

                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");

                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HtmlGenericControl hgc = (HtmlGenericControl)e.Row.FindControl("divHtml");
                hgc.InnerHtml = string.Format("<a href=\"Result.aspx?ID={0}\" target=\"_blank\">点击查看</a>", ((Label)e.Row.FindControl("lbCNO")).Text.Trim());
            }
        }
    }
}
